import '../providers/mood_provider.dart';

class Memory {
  final String id;
  final String title;
  final String description;
  final String content;
  final DateTime date;
  final String? imageUrl;
  final List<String> imagePaths;
  final int imageCount;
  final String author; // 👩 或 👨
  final MoodType? mood;
  final List<String> tags;

  Memory({
    required this.id,
    required this.title,
    required this.description,
    required this.content,
    required this.date,
    this.imageUrl,
    this.imagePaths = const [],
    this.imageCount = 1,
    required this.author,
    this.mood,
    this.tags = const [],
  });

  // 检查是否有图片
  bool get hasImages => imagePaths.isNotEmpty || imageUrl != null;

  Map<String, dynamic> toJson() {
    return {
      'id': id,
      'title': title,
      'description': description,
      'content': content,
      'date': date.toIso8601String(),
      'imageUrl': imageUrl,
      'imagePaths': imagePaths,
      'imageCount': imageCount,
      'author': author,
      'mood': mood?.name,
      'tags': tags,
    };
  }

  factory Memory.fromJson(Map<String, dynamic> json) {
    return Memory(
      id: json['id'],
      title: json['title'],
      description: json['description'],
      content: json['content'] ?? json['description'] ?? '',
      date: DateTime.parse(json['date']),
      imageUrl: json['imageUrl'],
      imagePaths: List<String>.from(json['imagePaths'] ?? []),
      imageCount: json['imageCount'] ?? 1,
      author: json['author'],
      mood: json['mood'] != null ? MoodType.values.firstWhere(
        (e) => e.name == json['mood'],
        orElse: () => MoodType.happy,
      ) : null,
      tags: List<String>.from(json['tags'] ?? []),
    );
  }
}